﻿using System;
using eCommerce.core;
using eCommerce.core.AdminUser;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;

namespace eCommerce.cms.Controls.AdminUser
{
    public partial class Infomation : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {                           
            if (!IsPostBack)
            {
                txtUserName.ReadOnly = true;
                ltMessage.Text = Utils.ShowMessage(string.Format("Thành viên {0}", CurrentUser.FullName), MessageType.Information);
                BindUser();
            }
        }

        private void BindUser()
        {
            var o = AdminUserController.AdminUser_GetById(CurrentUser.AdminUserId);
            if (o != null)
            {
                txtUserName.Text    = o.Username;
                txtEmail.Text       = o.Email;
                txtFullName.Text    = o.FullName;                
                txtInformation.Text = o.Information;
                txtBirthday.Text    = Utils.FormatIn_DDMMYYYY(o.Birthday);                                                

                GetGender(o);
            }
        }        

        private void GetGender(AdminUserObject o)
        {
            if (o.Gender)
            {
                rbMale.Checked = true;
                rbFemale.Checked = false;
            }
            else
            {
                rbMale.Checked = false;
                rbFemale.Checked = true;
            }
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            var o = AdminUserController.AdminUser_GetById(CurrentUser.AdminUserId);
            o.AdminUserId   = CurrentUser.AdminUserId;
            o.Email         = txtEmail.Text;
            o.FullName      = txtFullName.Text;
            o.Birthday      = Utils.ValidDateTime(txtBirthday.Text);
            o.Gender        = rbMale.Checked;
            o.Information   = txtInformation.Text;            

            AdminUserController.AdminUser_UpdateInfomation(o);

            ltMessage.Text = Utils.ShowMessage("Thông tin đã được cập nhật", MessageType.Information);
        }
    }
}